package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.AssetsTypeMapper;
import com.ly.heart.domain.AssetsTypeEntity;
import com.ly.heart.service.IAssetsTypeService;

import javax.annotation.Resource;

/**
 * 资产类别Service业务层处理
 *
 * @author cxl
 * @date 2024-09-03
 */
@Service
public class AssetsTypeServiceImpl extends ServiceImpl<AssetsTypeMapper,AssetsTypeEntity> implements IAssetsTypeService {

    @Resource
    private AssetsTypeMapper assetsTypeMapper;

    @Override
    public AjaxResult getAssetsTypeList(AssetsTypeEntity assetsType) {
        List<AssetsTypeEntity> assetsTypeList = assetsTypeMapper.selectAssetsTypeEntityList(assetsType);
        Page<AssetsTypeEntity> page = new Page<>(assetsType.getPage(),assetsType.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,assetsTypeList));
    }

    /**
     * 查询资产类别
     *
     * @param id 资产类别ID
     * @return 资产类别
     */
    @Override
    public AssetsTypeEntity selectAssetsTypeEntityById(String id)
    {
        return assetsTypeMapper.selectAssetsTypeEntityById(id);
    }

    /**
     * 查询资产类别列表
     *
     * @param assetsTypeEntity 资产类别
     * @return 资产类别
     */
    @Override
    public List<AssetsTypeEntity> selectAssetsTypeEntityList(AssetsTypeEntity assetsTypeEntity)
    {
        return assetsTypeMapper.selectAssetsTypeEntityList(assetsTypeEntity);
    }

    /**
     * 新增资产类别
     *
     * @param assetsType 资产类别
     * @return 结果
     */
    @Override
    public int insertAssetsTypeEntity(AssetsTypeEntity assetsType)
    {
        assetsType.setId(SnowflakeIdWorker.getInstance().nextStringId());
        assetsType.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        assetsType.setCreateTime(DateUtils.getNowDate());
        return assetsTypeMapper.insertAssetsTypeEntity(assetsType);
    }

    /**
     * 修改资产类别
     *
     * @param assetsType 资产类别
     * @return 结果
     */
    @Override
    public int updateAssetsTypeEntity(AssetsTypeEntity assetsType)
    {
        assetsType.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        assetsType.setUpdateTime(DateUtils.getNowDate());
        return assetsTypeMapper.updateAssetsTypeEntity(assetsType);
    }

    /**
     * 批量删除资产类别
     *
     * @param ids 需要删除的资产类别ID
     * @return 结果
     */
    @Override
    public int deleteAssetsTypeEntityByIds(String[] ids)
    {
        return assetsTypeMapper.deleteAssetsTypeEntityByIds(ids);
    }

    /**
     * 删除资产类别信息
     *
     * @param id 资产类别ID
     * @return 结果
     */
    @Override
    public int deleteAssetsTypeEntityById(String id)
    {
        return assetsTypeMapper.deleteAssetsTypeEntityById(id);
    }

    /**
     * 禁用/反禁用
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult disable ( String id ) {
        AssetsTypeEntity assetsType = this.getById(id);
        if ( assetsType == null ) {
            throw new CustomException( "数据不存在！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<AssetsTypeEntity>().
                set ( AssetsTypeEntity :: getIsDisabled, assetsType.getIsDisabled () == 1 ? 2 : 1 ).
                set ( AssetsTypeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( AssetsTypeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( AssetsTypeEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( assetsType.getIsDisabled () == 1 ? "禁用失败！" : "解禁失败！" );
        }
        if( assetsType.getIsDisabled () == 1 ){
            this.update(new LambdaUpdateWrapper<AssetsTypeEntity>().eq(AssetsTypeEntity::getId, id)
                    .set(AssetsTypeEntity::getUnbanBy, SecurityUtils.getLoginUser().getUser().getUserId())
                    .set(AssetsTypeEntity::getUnbanDept, CurrentUserUtil.getDeptId()));
        }
        if(assetsType.getIsDisabled() == 2){
            this.update(new LambdaUpdateWrapper<AssetsTypeEntity>().eq(AssetsTypeEntity::getId, id)
                    .set(AssetsTypeEntity::getDisableBy, SecurityUtils.getLoginUser().getUser().getUserId())
                    .set(AssetsTypeEntity::getDisableDept, CurrentUserUtil.getDeptId()));
        }
        return AjaxResult.success ( assetsType.getIsDisabled () == 1 ? "禁用成功" : "解禁成功" );
    }


}
